*******************************************************************************;
*******************************************************************************;
*This do-file replicates the following Tables and Figures:
* Tables 3-4 of the main text;
* Figures 1-10 of the main text;
* Appendix Figures B1-B2;
*******************************************************************************;
*******************************************************************************;

#delimit ;
clear all;
set more 1;
set maxvar 32000;
version 12;
local seed = 735;

*******************************************************************************;
*directories;
*******************************************************************************;

global upper_dir = "";

global scratch_dir = "$upper_dir\scratch";

global paper_dir = "$upper_dir\tables_figures";

global nlsy_data_dir = "$upper_dir\data";

global dofile_dir = "$upper_dir\stata_code";

*******************************************************************************;
*Counterfactual Analaysis
*******************************************************************************;

#delimit;
set output proc;

local set_n_draws = 10000;

forvalues j = 0(1)1{;

*upload estimated parameters;
cd "$scratch_dir";
use boot_collect_ols`j'_restricted0_new , clear;
    
*Use only the point estimates (b=0);
keep if boot_sample==0;

expand `set_n_draws';

****************************;
*Draw Initial Conditions;
****************************; 
set seed 1;

matrix M =  0 , 0 , 0 , mean_income_age5[1] ;
matrix V = ( var_child_cog[1]            ,   cov_child_cog_mom_cog[1]  ,  cov_child_cog_mom_noncog[1] ,  cov_child_cog_log_inc[1] \
			 cov_child_cog_mom_cog[1]    ,   var_mom_cog[1]            ,  cov_mom_cog_mom_noncog[1]   ,  cov_mom_cog_log_inc[1] \  
			 cov_child_cog_mom_noncog[1] ,	 cov_mom_cog_mom_noncog[1] ,  var_mom_noncog[1]           ,  cov_mom_noncog_log_inc[1] \  
             cov_child_cog_log_inc[1]    ,   cov_mom_cog_log_inc[1]    ,  cov_mom_noncog_log_inc[1]   ,  var_faminc5[1]   );

drawnorm log_child_skills_age5   mom_log_cogn_skills  mom_log_noncogn_skills   log_income_age5 , n(`set_n_draws') cov(V) means(M);

**************************************;
*Draw Shocks for Inv and Technology;
**************************************; 
forvalues t = 5(2)11{;

gen eta_inv_age`t'    = rnormal(0,1);
gen eta_skills_age`t' = rnormal(0,1);
gen eta_income_age`t' = rnormal(0,1);

qui sum eta_inv_age`t';
replace eta_inv_age`t' = (eta_inv_age`t' - r(mean))/r(sd);

qui sum eta_skills_age`t';
replace eta_skills_age`t' = (eta_skills_age`t' - r(mean))/r(sd);

qui sum eta_income_age`t';
replace eta_income_age`t' = (eta_income_age`t' - r(mean))/r(sd);

};

gen eta_school      = rnormal(0,1); 
qui sum eta_school;
replace eta_school  = (eta_school - r(mean))/r(sd);

gen eta_log_wage     = rnormal(0,1); 
qui sum eta_log_wage;
replace eta_log_wage = (eta_log_wage - r(mean))/r(sd);

*Standard Deviation of school and wage shocks;
gen std_school_shocks 	  = sqrt(alpha_anchor2);
gen std_log_wage_shocks   = sqrt(alpha_anchor2_wage);


*****************************************************************************;
*****************************************************************************;
*Simulate Baseline Economy;
*****************************************************************************;
*****************************************************************************;

forvalues t = 5(2)11 {;

***********************;
*Log-Investments;
***********************;

gen log_inv_age`t' =  alpha1_age`t'*log_child_skills_age`t'  + 
					  alpha2_age`t'*log_income_age`t'        +
					  alpha3_age`t'*mom_log_cogn_skills      +
					  alpha4_age`t'*mom_log_noncogn_skills   +
					  std_inv_age`t'*eta_inv_age`t';
					  
***********************;
*Log-Skills at t+2;
***********************;

local tp2 = `t' + 2;

gen log_child_skills_age`tp2' = log_TFP_age`t'                        				 +
								gamma1_age`t'*log_child_skills_age`t'  				 +
				 	  			gamma2_age`t'*log_inv_age`t'		   				 +
								gamma3_age`t'*log_child_skills_age`t'*log_inv_age`t' +
								std_prod_age`t'*eta_skills_age`t';

																
***********************;
*Log-Income at t+2;
***********************;

gen log_income_age`tp2' = 	inc_beta_const	                   +  
							inc_beta_tm2*log_income_age`t'     +
							income_shock_std*eta_income_age`t' ;
						
};

if `j'==0{;


preserve;

keep gamma2_age5 gamma3_age5 var_child_cog ; 
keep in 1/1000;

qui sum var_child_cog;

local sd_child_cogn = sqrt(r(mean));

local lb = -1.5*`sd_child_cogn';
local ub =  1.5*`sd_child_cogn';

gen elasticity_skills_age7_inv = . ;
gen log_child_skills_age5      = . ;
local ind_l = 1;
forvalues sk = `lb'(0.1)`ub'{;

replace elasticity_skills_age7_inv = gamma2_age5 + gamma3_age5*`sk' in `ind_l';
replace log_child_skills_age5      = `sk' in `ind_l' ;
local ind_l = `ind_l' + 1 ;
};


qui sum elasticity_skills_age7_inv, de;

line elasticity_skills_age7_inv log_child_skills_age5 ,
lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0)
ytitle("Elasticity" )  
xtitle("Log Cognitive Skills") 
graphregion(fcolor(white)) 
;
graph export $paper_dir\distribution_cskills1.png, replace;
graph export $paper_dir\distribution_cskills1.eps, replace;

restore;

preserve;

keep log_TFP_age* log_child_skills_age5 log_child_skills_age7 log_child_skills_age9 log_child_skills_age11 log_child_skills_age13;
gen i = _n;
reshape long log_TFP_age@ log_child_skills_age@, i(i) j(age) ;
collapse log_TFP_age  log_child_skills_age , by(age);

twoway connected log_TFP_age age if age<=11,
lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0)
ytitle("Log TFP" )  
xtitle("Child Age") 
graphregion(fcolor(white)) 
xlabel(5(2)11)
;
graph export $paper_dir\TFP.png, replace;
graph export $paper_dir\TFP.eps, replace;

twoway connected log_child_skills_age age,
lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0)
ytitle("Mean Log Skills" )  
xtitle("Child Age") 
graphregion(fcolor(white)) 
xlabel(5(2)13)
;
graph export $paper_dir\mean_log_skills.png, replace;
graph export $paper_dir\mean_log_skills.eps, replace;

restore;

twoway kdensity log_child_skills_age5 , lpattern(dash) lwidth(thick) lcolor(gs0) ||
kdensity log_child_skills_age13
, lpattern(shortdash) lwidth(thick) lcolor(gs6)
ytitle("Density" )  
xtitle("Log Cognitive Skills") 
graphregion(fcolor(white)) 
legend(lab(1 "Age 5-6") lab(2 "Age 13-14"))
;
graph export $paper_dir\distribution_cskills.png, replace;
graph export $paper_dir\distribution_cskills.eps, replace;

preserve;

keep log_child_skills_age5 log_child_skills_age7 log_child_skills_age9 log_child_skills_age11 log_child_skills_age13;

local list_percent 5 25 50 75 95 ;

foreach p in `list_percent'{;
gen p`p'_log_child_skills_age_5 = log_child_skills_age5;
gen p`p'_log_child_skills_age_7 = log_child_skills_age7;
gen p`p'_log_child_skills_age_9 = log_child_skills_age9;
gen p`p'_log_child_skills_age_11 = log_child_skills_age11;
gen p`p'_log_child_skills_age_13 = log_child_skills_age13;
};

drop log_child_skills_age*;

gen i = _n;
reshape long  p5_log_child_skills_age_@   p25_log_child_skills_age_@    p50_log_child_skills_age_@   p75_log_child_skills_age_@  p95_log_child_skills_age_@, i(i) j(age) ;

collapse  (p5) p5_log_child_skills_age* (p25)   p25_log_child_skills_age*  (p50) p50_log_child_skills_age* (p75) p75_log_child_skills_age*  (p95) p95_log_child_skills_age* , by(age);

twoway connected p5_log_child_skills_age age , lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
	   connected p25_log_child_skills_age age, lpattern(dash) msymbol(D)  lcolor(gs0) mcolor(gs0) ||
	   connected p50_log_child_skills_age age, lpattern(dash) msymbol(T)  lcolor(gs0) mcolor(gs0) ||
	   connected p75_log_child_skills_age age, lpattern(dash) msymbol(S)  lcolor(gs0) mcolor(gs0) ||
	   connected p95_log_child_skills_age age
,
lpattern(dash) msymbol(A)  lcolor(gs0) mcolor(gs0)
ytitle("Log Skills" )  
xtitle("Child Age") 
graphregion(fcolor(white)) 
xlabel(5(2)13)
legend(lab(1 "5th Percentile") lab(2 "25th Percentile") lab(3 "50th Percentile") lab(4 "75th Percentile") lab(5 "95th Percentile") )

;
graph export $paper_dir\Percentiles_Skills.png, replace;
graph export $paper_dir\Percentiles_Skills.eps, replace;

restore;



twoway kdensity log_inv_age5  , lpattern(dash) lwidth(thick) lcolor(gs0) ||
kdensity log_inv_age11
, lpattern(shortdash) lwidth(thick) lcolor(gs6)
ytitle("Density" )  
xtitle("Log Investment") 
graphregion(fcolor(white)) 
legend(lab(1 "Age 5-6") lab(2 "Age 13-14"))
;
graph export $paper_dir\distribution_inv.png, replace;
graph export $paper_dir\distribution_inv.eps, replace;





***************************************;
*Calculate Signal-Noise Ratios;
***************************************;

*Children;
#delimit;
forvalues t = 5(2)13{;
qui sum log_child_skills_age`t';
local var_skills_`t' = r(Var);

foreach x in  math comp recog {;
gen signal_`x'_`t' = lambda_`x'`t'^(2)*`var_skills_`t''/var_cogn_`x'`t';
gen noise_`x'_`t' = 1-signal_`x'_`t';
};
};

*Mothers;
#delimit;

*Cognitive;
foreach x in  asvab2 asvab3 asvab4 asvab5 asvab6 asvab8 {;
gen signal_`x' = lambda_`x'^(2)*var_mom_cog[1]/var_mc_`x';
gen noise_`x' = 1-signal_`x';
};

*Noncognitive;
foreach x in  se1 se2 se3 se4 se5 se6 se8 se9 se10 rotter1 rotter2 rotter3 rotter4  {;
gen signal_`x' = lambda_`x'^(2)*var_mom_noncog[1]/var_mnc_`x';
gen noise_`x' = 1-signal_`x';
};

};
***********************;
*Adult Outcomes;
***********************;

*Schooling;
gen school  = alpha_anchor0                         + 
			  alpha_anchor1*log_child_skills_age13  + 
			  std_school_shocks*eta_school;
			 
*Log-wage;
gen log_wage = 	alpha_anchor0_wage                         +
				alpha_anchor1_wage*log_child_skills_age13  +
				std_log_wage_shocks*eta_log_wage;
				
*****************************************************************************;
*****************************************************************************;
/*
Simulate counterfactuals: 
Income transfers at different ages;
*/
*****************************************************************************;
*****************************************************************************;
		
*Set Income Transfer: Income is in $10,000, so a transfer of 1 means a transfer of $10,000;		
local income_transfer = 1;
		
forvalues t_transfer = 5(2)11{;

*Initial Conditions;
gen log_child_skills_age5_`t_transfer' = log_child_skills_age5 ;
gen log_income_age5_`t_transfer'       = log_income_age5;

forvalues t = 5(2)11 {;


***********************;
/*
Policy (Income Transfer)
Income Transfer here only at time `t' (Income is in $10,000, so a transfer of 1 means a transfer of $10,000);
*/
***********************;

if `t'==`t_transfer'{;
*Generate Income in level (in $10,000);
gen income = exp(log_income_age`t'_`t_transfer');
*Generate new log-income that includes the transfer;
replace log_income_age`t'_`t_transfer' = log( income + `income_transfer');
drop income;
};

***********************;
*Log-Investments;
***********************;

gen log_inv_age`t'_`t_transfer' =  alpha1_age`t'*log_child_skills_age`t'_`t_transfer'  + 
								   alpha2_age`t'*log_income_age`t'_`t_transfer'        +
								   alpha3_age`t'*mom_log_cogn_skills                   +
								   alpha4_age`t'*mom_log_noncogn_skills                +
								   std_inv_age`t'*eta_inv_age`t';
					  
***********************;
*Log-Skills at t+2;
***********************;

local tp2 = `t' + 2;

gen log_child_skills_age`tp2'_`t_transfer' = log_TFP_age`t'                        										     +
											 gamma1_age`t'*log_child_skills_age`t'_`t_transfer'  							 +
											 gamma2_age`t'*log_inv_age`t'_`t_transfer'		   				 			     +
										     gamma3_age`t'*log_child_skills_age`t'_`t_transfer'*log_inv_age`t'_`t_transfer'  +
											 std_prod_age`t'*eta_skills_age`t';

***********************;
*Log-Income at t+2;
***********************;
	
/*	
if `t'==`t_transfer'{;
*Generate Income in level (in $10,000);
gen income = exp(log_income_age`t'_`t_transfer');
*Generate new log-income that includes the transfer;
replace log_income_age`t'_`t_transfer' = log( income - `income_transfer');
drop income;
};										
*/;

gen log_income_age`tp2'_`t_transfer' = 	inc_beta_const			   	                    +  
										inc_beta_tm2*log_income_age`t'_`t_transfer'     +
										income_shock_std*eta_income_age`t' ;


};

***********************;
*Adult Outcomes;
***********************;

*Schooling;
gen school_`t_transfer'  = alpha_anchor0                                      + 
						   alpha_anchor1*log_child_skills_age13_`t_transfer'  + 
						   std_school_shocks*eta_school;
*Log-wage;			 
gen log_wage_`t_transfer' = alpha_anchor0_wage                                     +
							alpha_anchor1_wage*log_child_skills_age13_`t_transfer' +
							std_log_wage_shocks*eta_log_wage;


};


****************************************;
*Calculating TEs;
****************************************;

forvalues t_transfer = 5(2)11{;

*Skills;
gen TE_final_skills_`t_transfer' = log_child_skills_age13_`t_transfer' - log_child_skills_age13;

gen TE_school_`t_transfer'         =  school_`t_transfer' - school ;
gen TE_school_months_`t_transfer'  = 12*TE_school_`t_transfer';
gen TE_log_wage_`t_transfer'       = log_wage_`t_transfer' - log_wage;
};

sum TE_final_skills_*;
sum TE_school_months_*;
sum TE_log_wage_*;

xtile income_percentiles = log_income_age5, nquantile(100);
xtile skills_percentiles = log_child_skills_age5, nquantile(100);


save counter_ols`j'_restricted0.dta, replace;

};


forvalues b = 1(1)100{;

set output p;
di "Iteration `b'";
set output e;


capture assert `b'==29;
 if ( _rc ) {;
forvalues j = 0(1)1{;

*upload estimated parameters;
cd "$scratch_dir";
use boot_collect_ols`j'_restricted0_new , clear;

*Use this specific bootstrap realization of the estimates;
keep if boot_sample==`b';

expand `set_n_draws';

****************************;
*Draw Initial Conditions;
****************************; 
set seed 1;

matrix M =  0 , 0 , 0 , mean_income_age5[1] ;
matrix V = ( var_child_cog[1]            ,   cov_child_cog_mom_cog[1]  ,  cov_child_cog_mom_noncog[1] ,  cov_child_cog_log_inc[1] \
			 cov_child_cog_mom_cog[1]    ,   var_mom_cog[1]            ,  cov_mom_cog_mom_noncog[1]   ,  cov_mom_cog_log_inc[1] \  
			 cov_child_cog_mom_noncog[1] ,	 cov_mom_cog_mom_noncog[1] ,  var_mom_noncog[1]           ,  cov_mom_noncog_log_inc[1] \  
             cov_child_cog_log_inc[1]    ,   cov_mom_cog_log_inc[1]    ,  cov_mom_noncog_log_inc[1]   ,  var_faminc5[1]   );

drawnorm log_child_skills_age5   mom_log_cogn_skills  mom_log_noncogn_skills   log_income_age5 , n(`set_n_draws') cov(V) means(M);

**************************************;
*Draw Shocks for Inv and Technology;
**************************************; 
forvalues t = 5(2)11{;

gen eta_inv_age`t'    = rnormal(0,1);
gen eta_skills_age`t' = rnormal(0,1);
gen eta_income_age`t' = rnormal(0,1);

qui sum eta_inv_age`t';
replace eta_inv_age`t' = (eta_inv_age`t' - r(mean))/r(sd);

qui sum eta_skills_age`t';
replace eta_skills_age`t' = (eta_skills_age`t' - r(mean))/r(sd);

qui sum eta_income_age`t';
replace eta_income_age`t' = (eta_income_age`t' - r(mean))/r(sd);

};

gen eta_school      = rnormal(0,1); 
qui sum eta_school;
replace eta_school  = (eta_school - r(mean))/r(sd);

gen eta_log_wage     = rnormal(0,1); 
qui sum eta_log_wage;
replace eta_log_wage = (eta_log_wage - r(mean))/r(sd);

*Standard Deviation of school and wage shocks;
gen std_school_shocks 	  = sqrt(alpha_anchor2);
gen std_log_wage_shocks   = sqrt(alpha_anchor2_wage);


*****************************************************************************;
*****************************************************************************;
*Simulate Baseline Economy;
*****************************************************************************;
*****************************************************************************;

forvalues t = 5(2)11 {;

***********************;
*Log-Investments;
***********************;

gen log_inv_age`t' =  alpha1_age`t'*log_child_skills_age`t'  + 
					  alpha2_age`t'*log_income_age`t'        +
					  alpha3_age`t'*mom_log_cogn_skills      +
					  alpha4_age`t'*mom_log_noncogn_skills   +
					  std_inv_age`t'*eta_inv_age`t';
					  
***********************;
*Log-Skills at t+2;
***********************;

local tp2 = `t' + 2;

gen log_child_skills_age`tp2' = log_TFP_age`t'                        				 +
								gamma1_age`t'*log_child_skills_age`t'  				 +
				 	  			gamma2_age`t'*log_inv_age`t'		   				 +
								gamma3_age`t'*log_child_skills_age`t'*log_inv_age`t' +
								std_prod_age`t'*eta_skills_age`t';

																
***********************;
*Log-Income at t+2;
***********************;

gen log_income_age`tp2' = 	inc_beta_const	                   +  
							inc_beta_tm2*log_income_age`t'     +
							income_shock_std*eta_income_age`t' ;
						
};

***********************;
*Adult Outcomes;
***********************;

*Schooling;
gen school  = alpha_anchor0                         + 
			  alpha_anchor1*log_child_skills_age13  + 
			  std_school_shocks*eta_school;
			 
*Log-wage;
gen log_wage = 	alpha_anchor0_wage                         +
				alpha_anchor1_wage*log_child_skills_age13  +
				std_log_wage_shocks*eta_log_wage;
				
*****************************************************************************;
*****************************************************************************;
/*
Simulate counterfactuals: 
Income transfers at different ages;
*/
*****************************************************************************;
*****************************************************************************;
		
*Set Income Transfer: Income is in $10,000, so a transfer of 1 means a transfer of $10,000;		
local income_transfer = 1;
		
forvalues t_transfer = 5(2)11{;

*Initial Conditions;
gen log_child_skills_age5_`t_transfer' = log_child_skills_age5 ;
gen log_income_age5_`t_transfer'       = log_income_age5;

forvalues t = 5(2)11 {;


***********************;
/*
Policy (Income Transfer)
Income Transfer here only at time `t' (Income is in $10,000, so a transfer of 1 means a transfer of $10,000);
*/
***********************;

if `t'==`t_transfer'{;
*Generate Income in level (in $10,000);
gen income = exp(log_income_age`t'_`t_transfer');
*Generate new log-income that includes the transfer;
replace log_income_age`t'_`t_transfer' = log( income + `income_transfer');
drop income;
};

***********************;
*Log-Investments;
***********************;

gen log_inv_age`t'_`t_transfer' =  alpha1_age`t'*log_child_skills_age`t'_`t_transfer'  + 
								   alpha2_age`t'*log_income_age`t'_`t_transfer'        +
								   alpha3_age`t'*mom_log_cogn_skills                   +
								   alpha4_age`t'*mom_log_noncogn_skills                +
								   std_inv_age`t'*eta_inv_age`t';
					  
***********************;
*Log-Skills at t+2;
***********************;

local tp2 = `t' + 2;

gen log_child_skills_age`tp2'_`t_transfer' = log_TFP_age`t'                        										     +
											 gamma1_age`t'*log_child_skills_age`t'_`t_transfer'  							 +
											 gamma2_age`t'*log_inv_age`t'_`t_transfer'		   				 			     +
										     gamma3_age`t'*log_child_skills_age`t'_`t_transfer'*log_inv_age`t'_`t_transfer'  +
											 std_prod_age`t'*eta_skills_age`t';

***********************;
*Log-Income at t+2;
***********************;
	
/*	
if `t'==`t_transfer'{;
*Generate Income in level (in $10,000);
gen income = exp(log_income_age`t'_`t_transfer');
*Generate new log-income that includes the transfer;
replace log_income_age`t'_`t_transfer' = log( income - `income_transfer');
drop income;
};										
*/;

gen log_income_age`tp2'_`t_transfer' = 	inc_beta_const			   	                    +  
										inc_beta_tm2*log_income_age`t'_`t_transfer'     +
										income_shock_std*eta_income_age`t' ;


};

***********************;
*Adult Outcomes;
***********************;

*Schooling;
gen school_`t_transfer'  = alpha_anchor0                                      + 
						   alpha_anchor1*log_child_skills_age13_`t_transfer'  + 
						   std_school_shocks*eta_school;
*Log-wage;			 
gen log_wage_`t_transfer' = alpha_anchor0_wage                                     +
							alpha_anchor1_wage*log_child_skills_age13_`t_transfer' +
							std_log_wage_shocks*eta_log_wage;


};


****************************************;
*Calculating TEs;
****************************************;

forvalues t_transfer = 5(2)11{;

*Skills;
gen TE_final_skills_`t_transfer' = log_child_skills_age13_`t_transfer' - log_child_skills_age13;

gen TE_school_`t_transfer'         =  school_`t_transfer' - school ;
gen TE_school_months_`t_transfer'  = 12*TE_school_`t_transfer';
gen TE_log_wage_`t_transfer'       = log_wage_`t_transfer' - log_wage;
};

sum TE_final_skills_*;
sum TE_school_months_*;
sum TE_log_wage_*;

xtile income_percentiles = log_income_age5, nquantile(100);
xtile skills_percentiles = log_child_skills_age5, nquantile(100);


save counter_ols`j'_restricted0_boot`b'.dta, replace;

};
};
};


****************************************************************;
****************************************************************;
*Graphs and Tables;
****************************************************************;
****************************************************************;

*upload estimated parameters;

cd "$scratch_dir";
use counter_ols0_restricted0 , clear;

collapse TE_final_skills* TE_school_months_* ;
gen i = _n;
reshape long TE_final_skills_@ TE_school_months_@ , i(i) j(age);
drop i;
gen b = 0;
cd "$scratch_dir";
save temp_cunter_ols0_restricted0, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols0_restricted0_boot`b' , clear;

collapse TE_final_skills* TE_school_months_* ;
gen i = _n;
reshape long TE_final_skills_@ TE_school_months_@ , i(i) j(age);
drop i;

gen b = `b';

append using temp_cunter_ols0_restricted0;

save temp_cunter_ols0_restricted0, replace;
};
};


#delimit;
use temp_cunter_ols0_restricted0, clear;


gen ub_TE_final_skills=.;
gen lb_TE_final_skills=.;

gen ub_TE_school_months=.;
gen lb_TE_school_months=.;


levelsof age, local(age_levels); 
foreach a of local age_levels {;

foreach y in TE_final_skills TE_school_months {;

 *_pctile `y'_ if age==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if age==`a';
 *replace lb_`y' = r(r2) if age==`a'; 
 
 sum `y'_ if age==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y'_ if age==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if age==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if age==`a'; 
 
 };
 
 };


twoway connected TE_final_skills_ age if b==0, lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
rcap ub_TE_final_skills  lb_TE_final_skills age if b==0 ,  lcolor(gs0)
ytitle("Average change in final log-skills" )  
xtitle("Age at which Income Transfer is Given") 
graphregion(fcolor(white))  xlabel(5(2)11)
legend(off)
;
graph export $paper_dir\TE_byage_skills.png, replace;
graph export $paper_dir\TE_byage_skills.eps, replace;


twoway connected TE_school_months_ age if b==0, lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
rcap ub_TE_school_months  lb_TE_school_months age if b==0 , lcolor(gs0)
ytitle("Average change in children completed education (months)" )  
xtitle("Age at which Income Transfer is Given") 
graphregion(fcolor(white))  xlabel(5(2)11)
legend(off)
;
graph export $paper_dir\TE_byage_school.png, replace;
graph export $paper_dir\TE_byage_school.eps, replace;


cd "$scratch_dir";
use counter_ols0_restricted0 , clear;
qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(income_percentiles);
gen b = 0;
cd "$scratch_dir";
save temp_TE_school_months_5, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols0_restricted0_boot`b' , clear;

qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(income_percentiles);
gen b = `b';

append using temp_TE_school_months_5;

save temp_TE_school_months_5, replace;
};
};

gen ub_TE_school_months_5=.;
gen lb_TE_school_months_5=.;


levelsof income_percentiles, local(income_levels); 
foreach a of local income_levels {;

foreach y in TE_school_months_5 {;

 *_pctile `y' if income_percentiles==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if income_percentiles==`a';
 *replace lb_`y' = r(r2) if income_percentiles==`a'; 
 
 sum `y' if income_percentiles==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y' if income_percentiles==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if income_percentiles==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if income_percentiles==`a'; 
 
 };
 };

 
 
#delimit;
twoway rarea  ub_TE_school_months_5  lb_TE_school_months_5 income_percentiles if b==0 , color(gs12) ||
 connected TE_school_months_5 income_percentiles if b==0, lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
connected TE_school_months_5_mean income_percentiles if b==0 , lpattern(dash) msymbol(none)  lcolor(gs0) mcolor(gs0)
ytitle("Average change in children completed education (months)" )  
xtitle("Family income percentiles") 
graphregion(fcolor(white))
legend(  lab(2 "ATE by Income Percentiles") lab(3 "ATE") lab(1 "95% CI") order( 2 1 3 ) ring(0) position(2))
;
graph export $paper_dir\heter_TE_school_byincome.png, replace;
graph export $paper_dir\heter_TE_school_byincome.eps, replace;



#delimit;
cd "$scratch_dir";
use counter_ols0_restricted0 , clear;
qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5_mean TE_school_months_5 , by(skills_percentiles);
gen b = 0;
cd "$scratch_dir";
save temp_TE_school_months_5, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols0_restricted0_boot`b' , clear;

qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(skills_percentiles);
gen b = `b';

append using temp_TE_school_months_5;

save temp_TE_school_months_5, replace;
};
};

gen ub_TE_school_months_5=.;
gen lb_TE_school_months_5=.;


levelsof skills_percentiles, local(skills_percentiles); 
foreach a of local skills_percentiles {;

foreach y in TE_school_months_5 {;

 * _pctile `y' if skills_percentiles==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if skills_percentiles==`a';
 *replace lb_`y' = r(r2) if skills_percentiles==`a'; 
 
  sum `y' if skills_percentiles==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y' if skills_percentiles==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if skills_percentiles==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if skills_percentiles==`a'; 
 
 };
 };




#delimit;
twoway rarea  ub_TE_school_months_5  lb_TE_school_months_5 skills_percentiles if b==0 , color(gs12) ||
 connected TE_school_months_5 skills_percentiles if b==0, lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
connected TE_school_months_5_mean skills_percentiles if b==0 , lpattern(dash) msymbol(none)  lcolor(gs0) mcolor(gs0)
ytitle("Average change in children completed education (months)" )  
xtitle("Initial children skills percentiles") 
graphregion(fcolor(white))
legend(  lab(2 "ATE by Skills Percentiles") lab(3 "ATE") lab(1 "95% CI") order( 2 1 3 ) ring(0) position(2))
;
graph export $paper_dir\heter_TE_school_byskills.png, replace;
graph export $paper_dir\heter_TE_school_byskills.eps, replace;


***************************************************************************;
*Cost-Benefit Analysis for Income Transfers to Poor Children;
*Here we calculate the costs and benefits of the policy for POOR children;
***************************************************************************;
#delimit;
cd "$scratch_dir";
use counter_ols0_restricted0 , clear;


*Set Income Transfer: Income is in $10,000, so a transfer of 1 means a transfer of $10,000;		
local income_transfer = 1;

*Set an hypothetical future wage;
local adult_wage_level = 50000;

*Amount of transfered income;
gen direct_cost = 10000*`income_transfer';

*Set the subsample of interest;
local level_percent_poor = 10;

*Set discount rate and school fees;
local interest_rate = 0.02;
local annual_school_fees   = 10000;

forvalues t_transfer = 5(2)11{;


xtile income_percentiles_`t_transfer' = log_income_age`t_transfer', nquantile(100);

gen benefits_`t_transfer' = 0 if income_percentiles_`t_transfer'<=`level_percent_poor';

*Calculate PDV of future earnings (assuming working 40 years);
forvalues j = 0(1)40{;
replace benefits_`t_transfer' = benefits_`t_transfer' + (exp( log(`adult_wage_level') + TE_log_wage_`t_transfer' ) - `adult_wage_level')/( (1+`interest_rate')^(`j') ) if income_percentiles_`t_transfer'<=`level_percent_poor';
};

gen schooling_cost_`t_transfer' = `annual_school_fees'*TE_school_`t_transfer' if income_percentiles_`t_transfer'<=`level_percent_poor';

gen net_PDV_policy_`t_transfer' = benefits_`t_transfer' - direct_cost - schooling_cost_`t_transfer' if income_percentiles_`t_transfer'<=`level_percent_poor';

};

#delimit;

keep net_PDV_policy_* benefits_* direct_cost schooling_cost_*;
collapse net_PDV_policy_* benefits_* direct_cost schooling_cost_*;
gen i = _n;

reshape long net_PDV_policy_@ benefits_@ schooling_cost_@ , i(i) j(age_transfer);
drop i;
list;

gen b = 0;

cd "$scratch_dir";
save temp_cb_analysis.dta,replace;


forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
use counter_ols0_restricted0_boot`b' , clear;

*Set Income Transfer: Income is in $10,000, so a transfer of 1 means a transfer of $10,000;		
local income_transfer = 1;

*Set an hypothetical future wage;
local adult_wage_level = 50000;

*Amount of transfered income;
gen direct_cost = 10000*`income_transfer';

*Set the subsample of interest;
local level_percent_poor = 10;

*Set discount rate and school fees;
local interest_rate = 0.02;
local annual_school_fees   = 10000;

forvalues t_transfer = 5(2)11{;


xtile income_percentiles_`t_transfer' = log_income_age`t_transfer', nquantile(100);

gen benefits_`t_transfer' = 0 if income_percentiles_`t_transfer'<=`level_percent_poor';

*Calculate PDV of future earnings (assuming working 40 years);
forvalues j = 0(1)40{;
replace benefits_`t_transfer' = benefits_`t_transfer' + (exp( log(`adult_wage_level') + TE_log_wage_`t_transfer' ) - `adult_wage_level')/( (1+`interest_rate')^(`j') ) if income_percentiles_`t_transfer'<=`level_percent_poor';
};

gen schooling_cost_`t_transfer' = `annual_school_fees'*TE_school_`t_transfer' if income_percentiles_`t_transfer'<=`level_percent_poor';

gen net_PDV_policy_`t_transfer' = benefits_`t_transfer' - direct_cost - schooling_cost_`t_transfer' if income_percentiles_`t_transfer'<=`level_percent_poor';

};

#delimit;

keep net_PDV_policy_* benefits_* direct_cost schooling_cost_*;
collapse net_PDV_policy_* benefits_* direct_cost schooling_cost_*;
gen i = _n;

reshape long net_PDV_policy_@ benefits_@ schooling_cost_@ , i(i) j(age_transfer);
drop i;

gen b = `b';

append using temp_cb_analysis.dta;

save temp_cb_analysis.dta, replace;

};
};

#delimit;

use temp_cb_analysis.dta, clear;

foreach y in benefits net_PDV_policy schooling_cost {;
gen ub_`y' = .;
gen lb_`y' = .;

forvalues j = 5(1)11{;

 *_pctile `y'_ if age_transfer==`j', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if age_transfer==`j';
 *replace lb_`y' = r(r2) if age_transfer==`j'; 

qui sum `y'_ if age_transfer==`j' & b==0;
local mean_effect = r(mean);
qui sum `y'_ if age_transfer==`j' & b!=0;
replace ub_`y' = `mean_effect' + r(sd)*1.96 if age_transfer==`j';
replace lb_`y' = `mean_effect' - r(sd)*1.96 if age_transfer==`j';
 

};
};

keep if b==0;

file open resultsfile using "$paper_dir\cost_benefit_analysis.tex", write replace;
file write resultsfile "\begin{tabular}{lcccc}" _n;
file write resultsfile "\hline \hline" _n;
file write resultsfile "\\[0.2cm]" _n;
file write resultsfile " \shortstack{Age of \\ Intervention}  & \shortstack{Benefit on \\ PV Earnings}  &  \shortstack{Direct Cost \\ (Income Transfer)} &  Cost of Education  &  Net Benefit \\" _n;
file write resultsfile "& (\\$) & (\\$) & (\\$) & (\\$) \\" _n;
file write resultsfile "\hline" _n;
file write resultsfile "\\[0.2cm]" _n;

forvalues j = 1(1)4{;
local column1 : di %1.0f round(age_transfer[`j'], .01); 
local column2 : di %5.2f round(benefits_[`j'], .01);  
local column3 : di %5.0f round(direct_cost[`j'], .01);  
local column4 : di %3.2f round(schooling_cost_[`j'], .01); 
local column5 : di %10.2f round(net_PDV_policy_[`j'], .01);

file write resultsfile " `column1' & `column2' & `column3' & `column4' & `column5'  \\" _n;
};

file write resultsfile "\hline \hline" _n;
file write resultsfile "\end{tabular}";
file close resultsfile;


******************************************************************************;
*Table comparing policy effects with and without Measurement Error Correction;
******************************************************************************;



*******************************;
*OLS = 0;
*******************************;


#delimit;
cd "$scratch_dir";
use counter_ols0_restricted0 , clear;

collapse TE_final_skills* TE_school_months_* ;
gen i = _n;
reshape long TE_final_skills_@ TE_school_months_@ , i(i) j(age);
drop i;
gen b = 0;
cd "$scratch_dir";
save temp_cunter_ols0_restricted0, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols0_restricted0_boot`b' , clear;

collapse TE_final_skills* TE_school_months_* ;
gen i = _n;
reshape long TE_final_skills_@ TE_school_months_@ , i(i) j(age);
drop i;

gen b = `b';

append using temp_cunter_ols0_restricted0;

save temp_cunter_ols0_restricted0, replace;
};
};


#delimit;
use temp_cunter_ols0_restricted0, clear;


gen ub_TE_final_skills=.;
gen lb_TE_final_skills=.;

gen ub_TE_school_months=.;
gen lb_TE_school_months=.;


levelsof age, local(age_levels); 
foreach a of local age_levels {;

foreach y in TE_final_skills TE_school_months {;

 
 * _pctile `y'_ if age==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if age==`a';
 *replace lb_`y' = r(r2) if age==`a'; 
 
  sum `y'_ if age==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y'_ if age==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if age==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if age==`a'; 
 
 }; 
 };

keep if b==0;
keep age TE_school_months_ ub_TE_school_months lb_TE_school_months;
gen i = _n;
reshape wide TE_school_months_ ub_TE_school_months lb_TE_school_months, i(i) j(age);
drop i;
gen do_ols = 0;
cd "$scratch_dir";
save temp_table_counter.dta,replace;



*******************************;
*OLS = 1;
*******************************;

cd "$scratch_dir";
use counter_ols1_restricted0 , clear;

collapse TE_final_skills* TE_school_months_* ;
gen i = _n;
reshape long TE_final_skills_@ TE_school_months_@ , i(i) j(age);
drop i;
gen b = 0;
cd "$scratch_dir";
save temp_cunter_ols1_restricted0, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols1_restricted0_boot`b' , clear;

collapse TE_final_skills* TE_school_months_* ;
gen i = _n;
reshape long TE_final_skills_@ TE_school_months_@ , i(i) j(age);
drop i;

gen b = `b';

append using temp_cunter_ols1_restricted0;

save temp_cunter_ols1_restricted0, replace;
};
};


#delimit;
use temp_cunter_ols1_restricted0, clear;


gen ub_TE_final_skills=.;
gen lb_TE_final_skills=.;

gen ub_TE_school_months=.;
gen lb_TE_school_months=.;


levelsof age, local(age_levels); 
foreach a of local age_levels {;

foreach y in TE_final_skills TE_school_months {;

 
 *_pctile `y'_ if age==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if age==`a';
 *replace lb_`y' = r(r2) if age==`a'; 
 
 
 
 sum `y'_ if age==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y'_ if age==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if age==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if age==`a'; 
 }; 
 };

keep if b==0;
keep age TE_school_months_ ub_TE_school_months lb_TE_school_months;
gen i = _n;
reshape wide TE_school_months_ ub_TE_school_months lb_TE_school_months, i(i) j(age);
drop i;
gen do_ols = 1;

append using temp_table_counter;

save temp_table_counter.dta, replace;



****************************************;
*OLS=0;
****************************************;

#delimit;
cd "$scratch_dir";
use counter_ols0_restricted0 , clear;
qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(income_percentiles);
gen b = 0;
cd "$scratch_dir";
save temp_TE_school_months_5, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols0_restricted0_boot`b' , clear;

qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(income_percentiles);
gen b = `b';

append using temp_TE_school_months_5;

save temp_TE_school_months_5, replace;
};
};

gen ub_TE_school_months_5=.;
gen lb_TE_school_months_5=.;


levelsof income_percentiles, local(income_levels); 
foreach a of local income_levels {;

foreach y in TE_school_months_5 {;
 
 * _pctile `y' if income_percentiles==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if income_percentiles==`a';
 *replace lb_`y' = r(r2) if income_percentiles==`a'; 
 
 
  sum `y' if income_percentiles==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y' if income_percentiles==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if income_percentiles==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if income_percentiles==`a'; 
 
 
 
 };
 };


keep if b==0;
gen do_ols = 0;
cd "$scratch_dir";
save temp_table_counter_by_income.dta,replace;


****************************************;
*OLS=1;
****************************************;


cd "$scratch_dir";
use counter_ols1_restricted0 , clear;
qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(income_percentiles);
gen b = 0;
cd "$scratch_dir";
save temp_TE_school_months_5, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols1_restricted0_boot`b' , clear;

qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(income_percentiles);
gen b = `b';

append using temp_TE_school_months_5;

save temp_TE_school_months_5, replace;
};
};

gen ub_TE_school_months_5=.;
gen lb_TE_school_months_5=.;


levelsof income_percentiles, local(income_levels); 
foreach a of local income_levels {;

foreach y in TE_school_months_5 {;
 
 
  *_pctile `y' if income_percentiles==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if income_percentiles==`a';
 *replace lb_`y' = r(r2) if income_percentiles==`a'; 
 
  sum `y' if income_percentiles==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y' if income_percentiles==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if income_percentiles==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if income_percentiles==`a'; 
 };
 };


keep if b==0;
gen do_ols = 1;

append using temp_table_counter_by_income;
cd "$scratch_dir";
save temp_table_counter_by_income.dta ,replace;





#delimit;
use temp_table_counter.dta, clear;

file open resultsfile using "$paper_dir\ATE_bymodels_byestimators.tex", write replace;
file write resultsfile "\begin{tabular}{ccccc}" _n;
file write resultsfile "\hline \hline" _n;
file write resultsfile "\\[0.2cm]" _n;
file write resultsfile "\multicolumn{5}{l}{Panel A: ATE by Age of Income Transfer} \\"_n;
file write resultsfile "\hline" _n;
file write resultsfile "\\[0.2cm]" _n;
file write resultsfile "& \multicolumn{4}{c}{Age of Income Transfer (\\$ 10,000)} \\"_n;
file write resultsfile " Model  & Age 5-6  & Age 7-8  & Age 9-10 & Age 11-12    \\" _n;
file write resultsfile "\hline" _n;
file write resultsfile "\\[0.2cm]" _n;

forvalues j = 0(1)1{;
  
local ind_t = 1;  
forvalues t = 5(2)11{;
sum TE_school_months_`t' if do_ols ==`j';
local column`ind_t':  di %4.3f r(mean) ;

sum ub_TE_school_months`t'  if do_ols ==`j';
local column`ind_t'_ub :  di %4.3f r(mean) ;

sum lb_TE_school_months`t' if do_ols ==`j';
local column`ind_t'_lb :  di %4.3f r(mean) ;

local ind_t = `ind_t' + 1;
};
  

if `j'==0{;
file write resultsfile "   Measur Error Corrected   & `column1' & `column2' & `column3' & `column4'  \\[-0.1cm]" _n;
file write resultsfile "      & [`column1_lb',`column1_ub'] & [`column2_lb',`column2_ub'] & [`column3_lb',`column3_ub'] & [`column4_lb',`column4_ub']  \\[0.3cm]" _n;

};

if `j'==1{;
file write resultsfile "  Not Corrected for Measur Error   & `column1' & `column2' & `column3' & `column4'  \\[-0.1cm] " _n;
file write resultsfile "      & [`column1_lb',`column1_ub'] & [`column2_lb',`column2_ub'] & [`column3_lb',`column3_ub'] & [`column4_lb',`column4_ub']  \\" _n;

};

  
};

use temp_table_counter_by_income.dta, clear;


file write resultsfile "\\[0.2cm]" _n;
file write resultsfile "\multicolumn{5}{l}{Panel B: ATE at age 5-6 by Family Income} \\"_n;
file write resultsfile "\hline" _n;
file write resultsfile "\\[0.2cm]" _n;
file write resultsfile " & \multicolumn{2}{c}{\shortstack{ Low Income Families \\ (10th Income Percentile)}  }  &  \multicolumn{2}{c}{ \shortstack{ High Income Families \\ (90th Income Percentile)} } \\  \cline{2-5} "_n;
file write resultsfile "\\[0.2cm]" _n;

forvalues j = 0(1)1{;
  
local ind_c = 1;  
forvalues ind_p =  10(80)90 {;
sum TE_school_months_5 if do_ols ==`j' & income_percentiles==`ind_p';
local column`ind_c':  di %6.3f r(mean)  ;

sum ub_TE_school_months`t'  if do_ols ==`j' & income_percentiles==`ind_p';
local column`ind_c'_ub :  di %4.3f r(mean) ;

sum lb_TE_school_months`t' if do_ols ==`j' & income_percentiles==`ind_p';
local column`ind_c'_lb :  di %4.3f r(mean) ;


local ind_c = `ind_c' + 2;
};
  

if `j'==0{;
file write resultsfile " Measur Error Corrected & \multicolumn{2}{c}{`column1'}    &    \multicolumn{2}{c}{`column3'}    \\[-0.1cm]" _n;
file write resultsfile "      & \multicolumn{2}{c}{[`column1_lb',`column1_ub']} &   \multicolumn{2}{c}{[`column3_lb',`column3_ub'] }   \\[0.3cm]" _n;
};

if `j'==1{;
file write resultsfile " Not Corrected for Measur Error  & \multicolumn{2}{c}{`column1'}  &   \multicolumn{2}{c}{`column3'}   \\[-0.1cm]" _n;
file write resultsfile "      & \multicolumn{2}{c}{[`column1_lb',`column1_ub']} &   \multicolumn{2}{c}{[`column3_lb',`column3_ub']}    \\[0.3cm]" _n;

};

  
};

file write resultsfile "\hline \hline" _n;
file write resultsfile "\end{tabular}";
file close resultsfile;




*******************************************************************************;
*******************************************************************************;
*Graphs for Counterfactuals with Alternative Models;
*******************************************************************************;
*******************************************************************************;

*upload estimated parameters;
#delimit;
cd "$scratch_dir";
use temp_table_counter_by_income.dta , clear;
keep if do_ols ==1;
#delimit;
twoway rarea  ub_TE_school_months_5  lb_TE_school_months_5 income_percentiles if b==0 , color(gs12) ||
 connected TE_school_months_5 income_percentiles if b==0, lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
connected TE_school_months_5_mean income_percentiles if b==0 , lpattern(dash) msymbol(none)  lcolor(gs0) mcolor(gs0)
ytitle("Average change in children completed education (months)" )  
xtitle("Family income percentiles") 
graphregion(fcolor(white))
legend(  lab(2 "ATE by Income Percentiles") lab(3 "ATE") lab(1 "95% CI") order( 2 1 3 ) ring(0) position(2))
;
graph export $paper_dir\heter_TE_school_byincome_ols.png, replace;
graph export $paper_dir\heter_TE_school_byincome_ols.eps, replace;


#delimit;

#delimit;
cd "$scratch_dir";
use counter_ols1_restricted0 , clear;
qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5_mean TE_school_months_5 , by(skills_percentiles);
gen b = 0;
cd "$scratch_dir";
save temp_TE_school_months_5, replace;

forvalues b = 1(1)100{;
capture assert `b'==29;
 if ( _rc ) {;
cd "$scratch_dir";
use counter_ols1_restricted0_boot`b' , clear;

qui sum TE_school_months_5;
gen TE_school_months_5_mean = r(mean);
collapse TE_school_months_5 TE_school_months_5_mean , by(skills_percentiles);
gen b = `b';

append using temp_TE_school_months_5;

save temp_TE_school_months_5, replace;
};
};

gen ub_TE_school_months_5=.;
gen lb_TE_school_months_5=.;


levelsof skills_percentiles, local(skills_percentiles); 
foreach a of local skills_percentiles {;

foreach y in TE_school_months_5 {;
 
 * _pctile `y' if skills_percentiles==`a', p(2.5,97.5);
 
 *replace ub_`y' = r(r1) if skills_percentiles==`a';
 *replace lb_`y' = r(r2) if skills_percentiles==`a'; 
 
 
  sum `y' if skills_percentiles==`a' & b!=0;
 
 local sd_`y' = r(sd);
 
 qui sum `y' if skills_percentiles==`a' & b==0;
 
 replace ub_`y' = r(mean) + 1.96*`sd_`y'' if skills_percentiles==`a';
 replace lb_`y' = r(mean) - 1.96*`sd_`y'' if skills_percentiles==`a'; 
 
 };
 };

#delimit;
twoway rarea  ub_TE_school_months_5  lb_TE_school_months_5 skills_percentiles if b==0 , color(gs12) ||
 connected TE_school_months_5 skills_percentiles if b==0, lpattern(dash) msymbol(O)  lcolor(gs0) mcolor(gs0) ||
connected TE_school_months_5_mean skills_percentiles if b==0 , lpattern(dash) msymbol(none)  lcolor(gs0) mcolor(gs0)
ytitle("Average change in children completed education (months)" )  
xtitle("Initial children skills percentiles") 
graphregion(fcolor(white))
legend(  lab(2 "ATE by Skills Percentiles") lab(3 "ATE") lab(1 "95% CI") order( 2 1 3 ) ring(0) position(2))
;
graph export $paper_dir\heter_TE_school_byskills_ols.png, replace;
graph export $paper_dir\heter_TE_school_byskills_ols.eps, replace;


